<?php

namespace app\admin\controller;
use app\admin\model\Currency as CurrencyModel;

class Currency extends Admin
{
    private $postField = 'name,currency_name,price_currency,type,tokentx,today_price,current_price,exchange,transfer,status,business,exchange,ec_type,ec,transfer,tc_type,tc';

    function index(){
        if (!$this->request->isPost()){
            return view('index');
        }else{
            $limit  = $this->request->post('limit', 20, 'intval');
            $page = $this->request->post('page', 1, 'intval');
            $order  = $this->request->post('order', '', 'serach_in');	//排序字段
            $sort  = $this->request->post('sort', '', 'serach_in');		//排序方式

            $where = [];

            $orderby = ($sort && $order) ? $sort.' '.$order : 'id desc';
            //获取币种列表
            $res = CurrencyModel::getList($orderby,$limit,$page,formatWhere($where));
            foreach ($res['data'] as &$v){
                $v['acton'] = '';
                $v['acton'] .= $v['exchange'] == 1 ? '开启兑换 | ' : '关闭兑换 | ';
                $v['acton'] .= $v['transfer'] == 1 ? '开启划转 ' : '关闭划转';
            }

            $data['status'] = 200;
            $data['data'] = $res;
            return json($data);
        }
    }

    /**
     * 添加币种
     * @return \think\response\Json
     */
    function add(){
        $data = $this->request->only(explode(',',$this->postField),'post',null);
        $res = CurrencyModel::insertGetId($data);

        if(empty($res)) return json(['status'=>200,'data'=>$res,'msg'=>'添加失败']);
        return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
    }

    /**
     * 获取币种详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getEdit(){
        $id =  $this->request->post('id', '', 'serach_in');
        if(!$id) throw new ValidateException ('参数错误');
        $res = CurrencyModel::getDetails($id);
        return json(['status'=>200,'data'=>$res]);
    }

    /**
     * 修改币种
     * @return \think\response\Json
     */
    function edit(){
        $postField = $this->postField.',id';
        $data = $this->request->only(explode(',',$postField),'post',null);
        CurrencyModel::where('id',$data['id'])->update($data);
        return json(['status'=>200,'msg'=>'修改成功']);
    }

    /**
     * 删除币种
     * @return \think\response\Json
     */
    function delete(){
        $idx =  $this->request->post('id', '', 'serach_in');
        if(!$idx) throw new ValidateException ('参数错误');
        CurrencyModel::destroy(['id'=>explode(',',$idx)],true);
        return json(['status'=>200,'msg'=>'操作成功']);
    }

    /**
     * 用户可用币种
     * @return \think\response\Json
     */
    function available_currency(){
        $data = CurrencyModel::getAll(['status'=>1,'business'=>1])->toArray();
        return json(['status'=>200,'msg'=>'修改成功','data'=>$data]);
    }

}